package com.mz.ozy.activity.home;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mz.ozy.R;
import com.mz.ozy.activity.me.MyOrderActivity;
import com.mz.ozy.activity.me.MyOrderFragment;
import com.mz.ozy.activity.shop.FirmOrderActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.BookDetailBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.bean.OnlineListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.BookDetailPresenter;
import com.mz.ozy.mvp.presenter.PrivateCustomPresenter;
import com.mz.ozy.mvp.view.BookDetailContract;
import com.mz.ozy.mvp.view.PrivateCustomContract;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.dialog.BaseDialog;
import com.mz.ozy.widget.dialog.PaySelectDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BookDetailActivity extends BaseActivity<BookDetailContract.View, BookDetailPresenter> implements BookDetailContract.View {
    @BindView(R.id.tv_address_name)
    TextView mTvAddressName;
    @BindView(R.id.tv_address)
    TextView mTvPrivateAddress;
    @BindView(R.id.tv_phone)
    TextView mTvPrivatePhone;
    @BindView(R.id.tv_box_name)
    TextView mTvBoxName;
    @BindView(R.id.tv_book_space)
    TextView mTvBookSpace;
    @BindView(R.id.tv_book_time)
    TextView mTvBookTime;
    @BindView(R.id.tv_book_info)
    TextView mTvBookInfo;
    @BindView(R.id.tv_book_remark)
    TextView mTvBookRemark;
    @BindView(R.id.tv_cancel_book)
    TextView mTvCancelBook;
    @BindView(R.id.tv_book_pay)
    TextView mTvBookPay;
    @BindView(R.id.tv_pay_msg)
    TextView mTvPayMsg;

    private PayManager mPayManager;
    private String id;
    private String balance;
    private int payType;
    private String price;
    private BookDetailBean.BookMapBean order_map1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("预定详情");
        EventBus.getDefault().register(this);
        id = getIntent().getStringExtra("id");
        balance = getIntent().getStringExtra("balance");
        price = getIntent().getStringExtra("price");
        mPayManager = new PayManager(this);
        mPresenter.queryDetail(this, id);
    }

    @OnClick({R.id.tv_cancel_book, R.id.tv_book_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel_book:
                String message = "确定取消" + order_map1.getMealdata() + " " + order_map1.getMealtime() + "的" + order_map1.getBjname() + "的有机家宴?";
                final BaseDialog dialog = new BaseDialog(this, R.layout.dialog_private, Gravity.CENTER, true);
                ((TextView) dialog.getView(R.id.tv_dialog_content)).setText(message);
                ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("取消订座");
                dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.toggleDialog();
                    }
                });
                dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.cancelBook(BookDetailActivity.this, id + "");
                        dialog.toggleDialog();
                    }
                });
                dialog.toggleDialog();
                break;
            case R.id.tv_book_pay:
                showSportsHarmDialog(id, price, balance);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)) {
            ToastUtils.showToast("支付成功");
            EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_MY_BOOK, null));
            finish();

        }
    }

    private void showSportsHarmDialog(final String orderId, final String price, final String balance) {
        PaySelectDialog mSportsHarmDialog = new PaySelectDialog(this, 2);
        mSportsHarmDialog.setData("¥" + price, balance);
        mSportsHarmDialog.setOnDialogListener(new PaySelectDialog.onDialogListener() {
            @Override
            public void onConfirm(int type, String payPwd) {
                payType = type;
                mPresenter.onPay(BookDetailActivity.this, orderId, type + "", payPwd);
            }

            @Override
            public void onCancel() {
//                EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_MY_BOOK, null));
//                finish();
            }
        });
        mSportsHarmDialog.toggleDialog();
    }

    @Override
    public void handleFailResponse(NetBean bean) {
        if ("1083".equals(bean.getRespCode())) {
            Toast toast = new Toast(this);
            toast.setGravity(Gravity.CENTER, 0, 0);
            final View toastView = View.inflate(this, R.layout.toast_private_view, null);
            TextView tvPrivateMsg = toastView.findViewById(R.id.tv_private_msg);
            tvPrivateMsg.setText("距离就餐时间小于1小时不可取消,请联系客服");
            toast.setView(toastView);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        } else {
            super.handleFailResponse(bean);
        }
    }

    @Override
    public void cancelBookSuccess(OnlineListBean orderBean) {
        mPresenter.queryDetail(this, id);
    }

    @Override
    public void queryDetailSuccess(BookDetailBean orderBean) {
        final BookDetailBean.BookMapBean order_map = orderBean.getOrder_map();
        if (order_map != null) {
            order_map1 = order_map;
            mTvAddressName.setText(order_map.getDpname());
            mTvPrivateAddress.setText("地址：" + order_map.getAddress());
            mTvPrivatePhone.setText(order_map.getDpphone());
            mTvBoxName.setText(order_map.getBjname() + "(可坐" + order_map.getMiniman() + "-" + order_map.getMaxman() + "人)");
            mTvBookSpace.setText(order_map.getYman() + "人");
            mTvBookTime.setText(order_map.getMealdata() + " " + order_map.getMealtime());
            mTvBookInfo.setText(order_map.getName() + " " + order_map.getPhone());
            mTvBookRemark.setText(order_map.getRemark());

            if (order_map.getState() == 0) {
                mTvBookPay.setVisibility(View.VISIBLE);
                mTvPayMsg.setVisibility(View.VISIBLE);
            } else {
                mTvBookPay.setVisibility(View.GONE);
                mTvPayMsg.setVisibility(View.GONE);
            }
            if (order_map.getState() == 0 || order_map.getState() == 1) {
                mTvCancelBook.setVisibility(View.VISIBLE);
            } else {
                mTvCancelBook.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onPaySuccess(JSONObject orderBean) {
        if (payType == 0) { //余额支付
            ToastUtils.showToast("支付成功");
            EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_MY_BOOK, null));
            finish();
        } else if (payType == 2) {  //微信支付
            mPayManager.handleWxPay(orderBean);
        } else if (payType == 1) {  //支付宝支付
            mPayManager.handleAliPay(orderBean.optString("zfb"));
        }
    }

    @Override
    public void onPayError(String s) {
        ToastUtils.showToast("支付失败");
    }

}
